import 'package:flutter/material.dart';

class ColorfulButton extends StatefulWidget {
  const ColorfulButton({
    super.key,
    required this.child,
    this.height = 30,
    this.paddingLR = 20,
    this.style,
    this.bgColor,
    this.fgColor,
    this.bgColorHover,
    this.fgColorHover,
    this.alwaysHighlight=false,
    this.onTap,
  });

  final Widget child;

  final Color? bgColor;
  final Color? fgColor;
  final Color? bgColorHover;
  final Color? fgColorHover;

  final TextStyle? style;

  final double height;
  final double paddingLR;

  final bool alwaysHighlight;

  final VoidCallback? onTap;

  @override
  State<ColorfulButton> createState() => _ColorfulButtonState();
}

class _ColorfulButtonState extends State<ColorfulButton> {

  bool hovering = false;

  Widget colorful({required Widget child, required Color color}) {
    // 动态修改 child 的颜色
    Widget coloredChild = child;
    if (child is Icon) {
      // 如果 child 是 Icon，重新包装并设置颜色
      return Icon(
        child.icon,
        size: child.size,
        color: color, // 使用 fgColor 或默认颜色
      );
    } else if (child is Text) {
      // 如果 child 是 Text，重新包装并设置颜色
      return Text(
        child.data ?? '',
        // TextStyle(color: widget.fgColor ?? color)
        style: widget.style?.copyWith(
          color: color
        ) ?? TextStyle(color: color),
      );
    }
    return child;
  }

  @override
  Widget build(BuildContext context) {

    double radius = widget.height / 2;

    Color? bgColor = widget.bgColor;
    Color? fgColor = widget.fgColor;
    Color? bgColorHover = widget.bgColorHover;
    Color? fgColorHover = widget.fgColorHover;

    if (widget.alwaysHighlight) {
      bgColor = bgColorHover;
      fgColor = fgColorHover;
    }

    return SizedBox(
      height: widget.height,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: MouseRegion(
          onEnter: (e) {
            setState(() {
              hovering = true;
            });
          },
          onExit: (e) {
            setState(() {
              hovering = false;
            });
          },
          child: AnimatedContainer(
            duration: const Duration(milliseconds: 160),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(radius),
              color: hovering ? bgColorHover?.withOpacity(0.68) : bgColor, //widget.bgColor,
            ),

            child: Material(
              color: Colors.transparent,
              child: InkWell(
                splashColor: widget.bgColorHover, // 波浪颜色
                highlightColor: Colors.transparent,
                onTap: () {
                  // print("taped");
                  widget.onTap?.call();
                },
                // onLongPress: () {
                //   print("long pressed");
                // },
                child: Padding(
                  padding: EdgeInsets.fromLTRB(widget.paddingLR, 0, widget.paddingLR, 0),
                  child: colorful(
                    child: widget.child,
                    color: hovering
                      ? fgColorHover ?? Colors.white.withOpacity(0.86)
                      : fgColor ?? Colors.white.withOpacity(0.86)
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );

  }
}
